<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Think;
use Think\Auth;
class SubjectController extends Controller {
    /**
     * 科目列表页面
     * @author 田永强 loseboycn@qq.com
     */   
    function subject(){
        $subject = M('subject');
        $all_subject = $subject -> select();
        $this->assign('count',count($all_subject));
        $this->assign('all_subject',$all_subject);
    	$this->display();
    }

    /**
     * 章节列表页面
     * @author 田永强 loseboycn@qq.com
     */   
    function section_show(){
        if(IS_GET){
            $sub_code = I('get.sub_code');
            $subject_section = M('subject_section');
            $subject_section_result = $subject_section -> where('sub_code = "'. $sub_code . '"')->select();
            $this->assign('sub_code',$sub_code);
            $this->assign('count',count($subject_section_result));
            $this->assign('subject_section_result',$subject_section_result);
            $this->display();
        }
    }

    /**
     * 章节删除方法
     * @author 田永强 loseboycn@qq.com
     */   
    function delsection(){
        if(IS_POST && IS_AJAX){
            $sectionid = I('post.sectionid');
            $subject_section = M('subject_section');
            $result = $subject_section -> where('sectionid = "'. $sectionid . '"')->delete();
            if($result >= 0){
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failed');
            }
        }
    }

    /**
     * 资源列表页面
     * @author 田永强 loseboycn@qq.com
     */
    function resource(){
        $resource = M('resource');
        $all_resource = $resource -> select();
        $this->assign('count',count($all_resource));
        $this->assign('all_resource',$all_resource);
        $this->display();
    }

    /**
     * 科目添加页面
     * @author 田永强 loseboycn@qq.com
     */   
    function subject_add(){
        $this->display();
    }

    /**
     * 科目编辑页面
     * @author 田永强 loseboycn@qq.com
     */   
    function subject_edit(){
        if(IS_GET){
            $sub_code = I('get.sub_code');
            $subject = M('subject');
            $subject_result = $subject -> where('sub_code = "'. $sub_code . '"')->find();
            $this->assign('subject_result',$subject_result);
            $this->display();
        }
    }

    /**
     * 章节添加页面
     * @author 田永强 loseboycn@qq.com
     */   
    function section_add(){
        $sub_code = I('get.sub_code');
        $subject = M('subject');
        $subject_name = $subject -> where('sub_code = "' . $sub_code . '"' ) -> getField('ChName');
        $subject_section = M('subject_section');
        $subject_section_result = $subject_section -> where('sub_code = "'. $sub_code . '"')->select();
        $subject_section_result['0'] = array(
            'parentid' => 0,
            'chname'   => $subject_name,
        );
        $this->assign('sub_code',$sub_code);
        $this->assign('subject_section_result',$subject_section_result);
        $this->display();
    }

    /**
     * 添加科目方法
     * @author 田永强 loseboycn@qq.com
     */   
    function addsubject(){
        if(IS_AJAX && IS_POST){
            $subject = M('subject');
            $result = $subject -> where('chname = "' . I('post.chname') . '"') -> find();
            if($result){
                $this->ajaxReturn('exist');
            }
            $subject_info = array(
                'Sub_Code'   => I('post.sub_code'),
                'ChName'     => I('post.chname'), 
                'EnName'     => I('post.enname'),
                'intro'      => I('post.intro'),
                'status'     => 0,
                'OrderID'    => I('post.orderid'),
                'CreateDate' => date("Y-m-d H:i:s",time()),
            );

            $file_info = $this -> upload();
            if($file_info['status'] != 'success' ){
                $this->ajaxReturn($file_info['status']);
            }
            $subject_info['imgPath'] = $file_info['path'];

            $result = $subject -> add($subject_info);
            if($result >= 0){
                 $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failed');
            }
        }
    }

    /**
     * 添加章节方法
     * @author 田永强 loseboycn@qq.com
     */   
    function addsection(){
        if(IS_AJAX && IS_POST){
            $subject_section_info = array(
                'ParentID' => I('post.parentid'),
                'Sub_Code' => I('post.sub_code'),
                'ChName'   => I('post.chname'),
                'Intro'    => I('post.intro'),
                'OrderID'    => I('post.orderid'),
            );
            $subject_section = M('subject_section');
            $subject_section_result = $subject_section -> add($subject_section_info);
            if($subject_section_result >= 0){
                 $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failed');
            }
        }
    }

    /**
     * 发布科目方法
     * @author 田永强 loseboycn@qq.com
     */   
    function startsubject(){
        if(IS_AJAX && IS_POST){
            $subject = M('subject');
            $subject_info['status'] = 1;
            $result = $subject -> where('sub_code = "' . I('post.sub_code') . '"') -> save($subject_info);
            if($result >= 0){
                 $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failed');
            }
        }
    }

    /**
     * 下架科目方法
     * @author 田永强 loseboycn@qq.com
     */   
    function stopsubject(){
        if(IS_AJAX && IS_POST){
            $subject = M('subject');
            $subject_info['status'] = 0;
            $result = $subject -> where('sub_code = "' . I('post.sub_code') . '"') -> save($subject_info);
            if($result >= 0){
                 $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failed');
            }
        }
    }

    /**
     * 删除科目方法
     * @author 田永强 loseboycn@qq.com
     */   
    function deletesubject(){
        if(IS_AJAX && IS_POST){
            $subject = M('subject');
            $subject_section = M('subject_section');
            $subject_section_result = $subject_section -> where('sub_code = "' . I('post.sub_code') . '"' ) -> select();
            if(count($subject_section_result)>0){
                $this->ajaxReturn('该科目下存在章节，请先删除章节后再删除科目！');
            }
            $path = $subject -> where('sub_code = "' . I('post.sub_code') . '"' ) -> getField('imgPath');
            unlink($path);//删除封面文件
            $result = $subject -> where('sub_code = "' . I('post.sub_code') . '"') -> delete();
            if($result >= 0){
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failed');
            }
        }
    }

    /**
     * 编辑科目方法
     * @author 田永强 loseboycn@qq.com
     */   
    function editsubject(){
        if(IS_AJAX && IS_POST){
            $subject = M('subject');
            $subject_info = array(
                'ChName'     => I('post.chname'), 
                'EnName'     => I('post.enname'),
                'intro'      => I('post.intro'),
                'status'     => 0,
                'OrderID'    => I('post.orderid'),
                'CreateDate' => date("Y-m-d H:i:s",time()),
            );
            if(I('post.uploadfile')!=''){//上传封面
                $file_info = $this -> upload();
                //文件上传异常检测
                if($file_info['status'] != 'success'){
                    $this->ajaxReturn($file_info['status']);
                }
                $path = $subject -> where('sub_code = "' . I('post.sub_code') . '"' ) -> getField('imgPath');
                unlink($path);//删除旧封面文件
                $subject_info['imgPath'] = $file_info['path'];
            }
            $result = $subject -> where('sub_code = "' . I('post.sub_code') . '"') -> save($subject_info);
            if($result >= 0){
                 $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failed');
            }
        }
    }

    /**
     * 科目封面上传方法
     * @author 田永强 loseboycn@qq.com
     */  
    private function upload(){
        $config = array(
                'mimes'         =>  array(), //允许上传的文件MiMe类型
                'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
                'exts'          =>  array('jpg', 'jpeg','png'), //允许上传的文件后缀
                'autoSub'       =>  false, //自动子目录保存文件
                'subName'       =>  array('date', 'Y-m'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
                'rootPath'      =>  './Uploads/subject/', //保存根路径
                'savePath'      =>  '', //保存路径
                'saveName'      =>  array('uniqid',''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
                'saveExt'       =>  '', //文件保存后缀，空则使用原后缀
                'replace'       =>  false, //存在同名是否覆盖
                'hash'          =>  true, //是否生成hash编码
                'callback'      =>  false, //检测文件是否存在回调，如果存在返回文件信息数组
                'driver'        =>  '', // 文件上传驱动
                'driverConfig'  =>  array(), // 上传驱动配置
            );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info   =   $upload->upload();
        if(!$info) {
            //上传错误返回错误信息
            $file_info['status'] = $upload->getError();
            return $file_info;
        }else{
            //上传成功
            $file_info = array(
                'path' => "Uploads/subject/" . $info['file']['savename'],
                'status' => "success",
                );
            return $file_info;
        }
    }

 }